# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import os
from reedlive_scrapy.utils import developconf
from reedlive_scrapy.utils import productconf
from reedlive_scrapy.items import InformationItem,WeChatItem,ChannelItem,TuikuItem
import pymongo


class ReedliveScrapyPipeline(object):
    def process_item(self, item, spider):
        return item


class ReedLiveDBDtoragePipline(object):
    def __init__(self, mongo_url, mongo_db):
        # print ' --------setting----------- '
        self.mongo_url = mongo_url
        self.mongo_db = mongo_db

    @classmethod
    def from_crawler(cls, crawler):
        if os.environ["NODE_ENV"]=='develop':
            return cls(
                mongo_url=developconf.MONGODB_URL,
                mongo_db=developconf.MONGODB_DB
            )
        else:
            return cls(
                mongo_url=productconf.MONGODB_URL,
                mongo_db=productconf.MONGODB_DB
            )
    def open_spider(self, spider):
        self.client = pymongo.MongoClient(self.mongo_url)
        self.db = self.client[self.mongo_db]


    def close_spider(self, spider):
        self.client.close()

    def process_item(self, item, spider):
        # print ' --------running----------- '
        if isinstance(item, InformationItem):
            collection = self.db['rssitems']
            if not collection.find_one({'link': str(item['link'])}):
                collection.insert(dict(item))
        elif isinstance(item,TuikuItem):
            collection = self.db['rssitems']
            if not collection.find_one({'mid': str(item['mid'])}):
                collection.insert(dict(item))
        elif isinstance(item,WeChatItem):
            collection = self.db['rssitems']
            if not collection.find_one({'wid': str(item['wid'])}):
                collection.insert(dict(item))
        elif isinstance(item, ChannelItem):
            collection = self.db['rssChannel']
            if not collection.find_one({'title': str(item['title'])}):
                collection.insert(dict(item))
        return item
